﻿using Abp.Application.Services.Dto;
using Abp.Web.Models;
using KDS.Mes.Authorization.Roles.Dto;
using KDS.Mes.Roles;
using KDS.Mes.Web.Models.Roles;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace KDS.Mes.Web.Controllers
{
    public class RolesController : Controller
    {
        private readonly IRoleAppService _roleAppService;

        public RolesController(IRoleAppService roleAppService)
        {
            _roleAppService = roleAppService;

        }

        // GET: Mpa/Roles
        public ActionResult Index()
        {
            return View();
        }

        [DontWrapResult]
        public JsonResult GetAllRoles(int limit, int offset, string sortfiled, string sortway)
        {
            var sort = !string.IsNullOrEmpty(sortfiled) ? string.Format("{0} {1}", sortfiled, sortway) : "";
            var filter = new GetRolesInput
            {
                SkipCount = offset,
                MaxResultCount = limit,
                Sorting = sort,
            };
            var pagedTasks = _roleAppService.GetPagedRoles(filter);
            return Json(new { total = pagedTasks.TotalCount, rows = pagedTasks.Items }, JsonRequestBehavior.AllowGet);
        }


        public async Task<PartialViewResult> CreateOrEditModal(int? id)
        {
            var output = await _roleAppService.GetRoleForEdit(new NullableIdDto { Id = id });
            var viewModel = new CreateOrEditRoleModalViewModel(output);

            return PartialView("_CreateOrEditModal", viewModel);
        }


        // GET: Mpa/Roles/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Mpa/Roles/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Mpa/Roles/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Mpa/Roles/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: Mpa/Roles/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Mpa/Roles/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: Mpa/Roles/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
